package com.sky.web;

import cn.hutool.core.bean.BeanUtil;
import com.sky.api.member.MemberFeignService;
import com.sky.dto.member.UmsMemberVO;
import com.sky.utils.R;
import com.sky.vo.UserLoginVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController {

    @Resource
    private MemberFeignService memberFeignService;

    @PostMapping("/login")
    public String login(UserLoginVo userLoginVo, RedirectAttributes redirectAttributes) {
        UmsMemberVO vo = new UmsMemberVO();
        BeanUtil.copyProperties(userLoginVo, vo);

        Map<String, String> errors = new HashMap<>(16);
        R<UmsMemberVO> login = memberFeignService.login(vo);
        if (login.getCode() == 0){
            return "redirect:http://skymall.com";
        } else {
            errors.put("msg", login.getMsg());
            redirectAttributes.addFlashAttribute("errors", errors);
            return "redirect:/login.html";
        }
    }
}
